package com.bbx.taxi.client.UpdateChecker.baidu;

import java.io.File;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Message;
import android.view.View;
import android.widget.RemoteViews;

import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.baidu.autoupdatesdk.CPUpdateDownloadCallback;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.taxi.client.Activity.login.TransparentActivity;
import com.bbx.taxi.client.UpdateChecker.ui.MyProgressBar;
import com.bbx.taxi.client.UpdateChecker.ui.MyUpdateDailog;
import com.bbx.taxi.official.R;

public class UpdateDownloadCallback implements CPUpdateDownloadCallback {
	private Context context;
	// 下载进度条


	private MyProgressBar progressBar;
	private Notification notify;
	private NotificationManager mNotifyMgr;
    private boolean Forced;
	public UpdateDownloadCallback(Context context, boolean Forced) {
		this.context = context;
		this.Forced=Forced;
		mNotifyMgr = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		if (Forced) {
			final MyUpdateDailog dialog = new MyUpdateDailog(context);
			dialog.show();
			progressBar = dialog.getProgressBar();
		}else{
			showNofity();
		}

	}

	@Override
	public void onDownloadComplete(String apkPath) {
		ToastUtil.showToast(context, R.string.downloa_complete);
		BDAutoUpdateSDK.cpUpdateInstall(context, apkPath);
	}

	@Override
	public void onStart() {
		ToastUtil.showToast(context, R.string.downloa_start);
	}

	@Override
	public void onPercent(int percent, long rcvLen, long fileSize) {
		// 下载进度


		// 更新进度情况


		if (progressBar != null) {
			progressBar.setProgress(percent);
			if(percent==100){
				progressBar.setVisibility(View.GONE);
			}
		} else {
			if(!Forced){
			updateDownloadNotify(percent);
			}
		}
	}

	@Override
	public void onFail(Throwable error, String content) {
		ToastUtil.showToast(context, R.string.downloa_fail);
	}

	@Override
	public void onStop() {
		if(!Forced){
			downloadCompleteNotify();	
		}
	
		ToastUtil.showToast(context, R.string.downloa_stop);
	}

	/** 准备升级notify */
	private void showNofity() {
		notify = getNotify(R.string.upgrade);
		RemoteViews view = new RemoteViews(context.getPackageName(),
				R.layout.notify_upgrade);
		view.setTextViewText(R.id.textprogress,
				context.getString(R.string.download_ready));
		view.setTextViewText(R.id.download_complete, "");
		view.setProgressBar(R.id.progressBar1, 100, 0, false);

		notify.contentView = view;
		Intent intent = new Intent();
		intent.setClass(context, TransparentActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

		PendingIntent ci = PendingIntent.getActivity(context, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notify.flags |= Notification.FLAG_AUTO_CANCEL;
		notify.contentIntent = ci;
		mNotifyMgr.notify(R.drawable.ic_launcher, notify);
	}

	public Notification getNotify(int title) {
		return new Notification(R.drawable.ic_launcher,
				context.getString(title), System.currentTimeMillis());
	}

	/** 更新notify */
	private void updateDownloadNotify(int progress) {
		RemoteViews view;
		if (notify == null) {
			notify = new Notification(R.drawable.ic_launcher,
					context.getString(R.string.upgrade),
					System.currentTimeMillis());
			view = new RemoteViews(context.getPackageName(),
					R.layout.notify_upgrade);
			notify.contentView = view;
		} else {
			view = notify.contentView;
		}
		view.setTextViewText(R.id.download_complete, "");
		int downloadProgress = progress;
		if (downloadProgress > 100)
			downloadProgress = 99;// 超过100，则处理为99


		view.setTextViewText(R.id.textprogress,
				context.getString(R.string.app_name) + "(" + downloadProgress
						+ "%)");
		view.setProgressBar(R.id.progressBar1, 100, progress, false);

		mNotifyMgr.notify(R.drawable.ic_launcher, notify);
	}

	/** 下载完成 */
	private void downloadCompleteNotify() {
		mNotifyMgr.cancel(R.drawable.ic_launcher);
	}
}